package com.cgsd.heyu.ad;

import android.app.Activity;
import android.view.View;

import com.android.library.utils.BeanUtils;
import com.android.library.utils.DpPxUtil;
import com.android.library.utils.SystemUtil;
import com.apkfuns.logutils.LogUtils;
import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.bytedance.sdk.openadsdk.TTNativeExpressAd;
import com.cgsd.heyu.App;
import com.cgsd.heyu.api.AppConfig;
import com.cgsd.heyu.entity.AdContentEntity;

import java.lang.ref.WeakReference;
import java.util.List;

/**
 * @author xiaoxing
 * Description 插屏广告
 * CreateData: 2020/11/24
 */
public class AdInputScreen {
    /**
     * 开屏广告加载超时时间,建议大于3000,这里为了冷启动第一次加载到广告并且展示,示例设置了3000ms
     */
    private static final int AD_TIME_OUT = 3000;
    private TTAdNative mTTAdNative;

    private AdContentEntity adContentEntity;
    private WeakReference<Activity> weakReference;
    private AdListener adListener;
    private TTNativeExpressAd mTTAd;
    private int width;
    private int height;


    public AdInputScreen(Activity activity, AdContentEntity entity, AdListener adListener){
        this.adListener = adListener;
        weakReference = new WeakReference<>(activity);
        this.adContentEntity = entity;

        if (BeanUtils.isEmpty(adContentEntity)) {
            return;
        }
        if (AdContentEntity.CHUANSHANJIA.equals(adContentEntity.platformType)) {
            mTTAdNative = TTAdSdk.getAdManager().createAdNative(weakReference.get());;
        }
        width = (int) (SystemUtil.getScreenWidth(App.getInstance()) * 0.8);
        height = width * 3 / 2;
    }


    /**
     * 加载广告
     */
    public void loadAd(){
        if (AdContentEntity.GUANGDIANTONG.equals(adContentEntity.platformType)) {
            adRewardVideoG();
        } else {
            adRewardVideoC();
        }
    }

    /**
     * 加载广点通开屏甲短视频广搞
     */
    private void adRewardVideoG(){

    }

    /**
     * 加载穿山开屏甲短视频广搞
     */
    private void adRewardVideoC(){
        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId(adContentEntity.advertisingId)
                .setSupportDeepLink(true)
                //请求广告数量为1到3条
                .setAdCount(1)
                //期望个性化模板广告view的size,单位dp
                .setExpressViewAcceptedSize(DpPxUtil.px2dip(App.getInstance(),width),DpPxUtil.px2dip(App.getInstance(),height))
                //这个参数设置即可，不影响个性化模板广告的size
                .setImageAcceptedSize(640,320 )
                .setUserID(AppConfig.AD_USER_ID)
                .build();
        mTTAdNative.loadInteractionExpressAd(adSlot, new TTAdNative.NativeExpressAdListener() {

            @Override
            public void onError(int i, String s) {
                LogUtils.e("onError loadInteractionExpressAd"+",message="+s+",code="+i);
                if (adListener != null){
                    adListener.onFail();
                }
            }

            @Override
            public void onNativeExpressAdLoad(List<TTNativeExpressAd> ads) {
                if (ads == null || ads.size() == 0){
                    return;
                }
                mTTAd = ads.get(0);
                bindAdListener(mTTAd);
                //调用render开始渲染广告
                mTTAd.render();
            }
        });
    }

    /**
     * 绑定广告行为
     * @param ad
     */
    private void bindAdListener(TTNativeExpressAd ad) {
        ad.setExpressInteractionListener(new TTNativeExpressAd.AdInteractionListener() {

            @Override
            public void onAdDismiss() {
                //广告关闭
                if (adListener != null){
                    adListener.onVideoComplete();
                }
            }

            @Override
            public void onAdClicked(View view, int type) {
                //广告被点击
            }

            @Override
            public void onAdShow(View view, int type) {
                //广告展示
            }

            @Override
            public void onRenderFail(View view, String msg, int code) {
                if (adListener != null){
                    adListener.onFail();
                }
            }

            @Override
            public void onRenderSuccess(View view, float width, float height) {
                if (adListener != null){
                    adListener.onSuccess();
                }
                //在渲染成功回调时展示广告，提升体验
                mTTAd.showInteractionExpressAd(weakReference.get());
            }
        });
    }


    public void clear(){
        if (mTTAd != null) {
            //调用destroy()方法释放
            mTTAd.destroy();
        }
    }
}
